// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 2014/1/26 16:18:01
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   MybatisCriteriaPlugin.java

package org.mybatis.generator.plugins;

import java.util.*;
import org.mybatis.generator.api.*;
import org.mybatis.generator.api.dom.xml.*;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;
import org.mybatis.generator.config.*;
import org.mybatis.generator.internal.util.StringUtility;

public class MybatisCriteriaPlugin extends PluginAdapter
{

    public MybatisCriteriaPlugin()
    {
        commonFile = false;
    }

    public boolean validate(List warnings)
    {
        databaseType = context.getJdbcConnectionConfiguration().getDriverClass();
        if(StringUtility.stringHasValue(properties.getProperty("commonFile")))
            commonFile = StringUtility.isTrue(properties.getProperty("commonFile"));
        else
            commonFile = false;
        return true;
    }

    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable)
    {
        XmlElement Where_Clause = new XmlElement("sql");
        Where_Clause.addAttribute(new Attribute("id", "Example_Where_Clause"));
        StringBuilder sb = new StringBuilder();
        XmlElement dynamicElement = new XmlElement("trim");
        dynamicElement.addAttribute(new Attribute("prefix", "where"));
        dynamicElement.addAttribute(new Attribute("prefixOverrides", "and|or"));
        Where_Clause.addElement(dynamicElement);
        XmlElement isNotNullElement;
        for(Iterator iterator = introspectedTable.getNonPrimaryKeyColumns().iterator(); iterator.hasNext(); isNotNullElement.addElement(new TextElement(sb.toString())))
        {
            IntrospectedColumn introspectedColumn = (IntrospectedColumn)iterator.next();
            isNotNullElement = new XmlElement("if");
            String va = (new StringBuilder(String.valueOf(introspectedColumn.getJavaProperty("params.")))).append(" != null").toString();
            isNotNullElement.addAttribute(new Attribute("test", va));
            dynamicElement.addElement(isNotNullElement);
            sb.setLength(0);
            sb.append(" and ");
            sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
            sb.append(" = ");
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, "params."));
        }

        document.getRootElement().getElements().add(2, Where_Clause);
        document.getRootElement().getElements().add(3, createSelectByParams(introspectedTable));
        document.getRootElement().getElements().add(4, createSelectCountByParams(introspectedTable));
        document.getRootElement().getElements().add(5, createSelectListByParams(introspectedTable));
        return true;
    }

    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable)
    {
        if(databaseType.contains("oracle"))
        {
            XmlElement oracleHeadIncludeElement = new XmlElement("include");
            oracleHeadIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Head"));
            element.addElement(0, oracleHeadIncludeElement);
            XmlElement orderIncludeElement = new XmlElement("if");
            orderIncludeElement.addAttribute(new Attribute("test", "orderParam != null"));
            orderIncludeElement.addElement(new TextElement("order by ${orderParam}"));
            element.addElement(element.getElements().size(), orderIncludeElement);
            XmlElement oracleFootIncludeElement = new XmlElement("include");
            oracleFootIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Foot"));
            element.addElement(element.getElements().size(), oracleFootIncludeElement);
        } else
        if(databaseType.contains("mysql"))
        {
            XmlElement orderIncludeElement = new XmlElement("if");
            orderIncludeElement.addAttribute(new Attribute("test", "orderParam != null"));
            orderIncludeElement.addElement(new TextElement("order by ${orderParam}"));
            element.addElement(element.getElements().size(), orderIncludeElement);
            XmlElement mysqlLimitIncludeElement = new XmlElement("include");
            mysqlLimitIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Limit"));
            element.addElement(element.getElements().size(), mysqlLimitIncludeElement);
        }
        return true;
    }

    public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable)
    {
        if(databaseType.contains("oracle"))
        {
            XmlElement oracleHeadIncludeElement = new XmlElement("include");
            oracleHeadIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Head"));
            element.addElement(0, oracleHeadIncludeElement);
            XmlElement orderIncludeElement = new XmlElement("if");
            orderIncludeElement.addAttribute(new Attribute("test", "orderParam != null"));
            orderIncludeElement.addElement(new TextElement("order by ${orderParam}"));
            element.addElement(element.getElements().size(), orderIncludeElement);
            XmlElement oracleFootIncludeElement = new XmlElement("include");
            oracleFootIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Foot"));
            element.addElement(element.getElements().size(), oracleFootIncludeElement);
        } else
        if(databaseType.contains("mysql"))
        {
            XmlElement orderIncludeElement = new XmlElement("if");
            orderIncludeElement.addAttribute(new Attribute("test", "orderParam != null"));
            orderIncludeElement.addElement(new TextElement("order by ${orderParam}"));
            element.addElement(element.getElements().size(), orderIncludeElement);
            XmlElement mysqlLimitIncludeElement = new XmlElement("include");
            mysqlLimitIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Limit"));
            element.addElement(element.getElements().size(), mysqlLimitIncludeElement);
        }
        return true;
    }

    private XmlElement createSelectByParams(IntrospectedTable introspectedTable)
    {
        String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
        StringBuffer sb = new StringBuffer();
        XmlElement selectCriteria = new XmlElement("select");
        selectCriteria.addAttribute(new Attribute("id", "selectByParams"));
        selectCriteria.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        selectCriteria.addElement(new TextElement("select"));
        XmlElement baseColumnIncludeElement = new XmlElement("include");
        baseColumnIncludeElement.addAttribute(new Attribute("refid", "Base_Column_List"));
        selectCriteria.addElement(baseColumnIncludeElement);
        sb.append("from ").append(tableName);
        selectCriteria.addElement(new TextElement(sb.toString()));
        XmlElement oracleWhereIncludeElement = new XmlElement("include");
        oracleWhereIncludeElement.addAttribute(new Attribute("refid", "Example_Where_Clause"));
        selectCriteria.addElement(oracleWhereIncludeElement);
        return selectCriteria;
    }

    private XmlElement createSelectCountByParams(IntrospectedTable introspectedTable)
    {
        String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
        StringBuffer sb = new StringBuffer();
        XmlElement selectCriteria = new XmlElement("select");
        selectCriteria.addAttribute(new Attribute("id", "selectCountByParams"));
        selectCriteria.addAttribute(new Attribute("resultType", "int"));
        sb.append("select").append(" count(*)").append(" from ").append(tableName);
        selectCriteria.addElement(new TextElement(sb.toString()));
        XmlElement oracleWhereIncludeElement = new XmlElement("include");
        oracleWhereIncludeElement.addAttribute(new Attribute("refid", "Example_Where_Clause"));
        selectCriteria.addElement(oracleWhereIncludeElement);
        return selectCriteria;
    }

    private XmlElement createSelectListByParams(IntrospectedTable introspectedTable)
    {
        String tableName = introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime();
        StringBuffer sb = new StringBuffer();
        XmlElement selectCriteria = new XmlElement("select");
        selectCriteria.addAttribute(new Attribute("id", "selectListByParams"));
        selectCriteria.addAttribute(new Attribute("resultType", "list"));
        selectCriteria.addAttribute(new Attribute("resultMap", "BaseResultMap"));
        if(databaseType.contains("oracle"))
        {
            XmlElement oracleHeadIncludeElement = new XmlElement("include");
            oracleHeadIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Head"));
            selectCriteria.addElement(oracleHeadIncludeElement);
            selectCriteria.addElement(new TextElement("select"));
            XmlElement baseColumnIncludeElement = new XmlElement("include");
            baseColumnIncludeElement.addAttribute(new Attribute("refid", "Base_Column_List"));
            selectCriteria.addElement(baseColumnIncludeElement);
            sb.append("from ").append(tableName);
            selectCriteria.addElement(new TextElement(sb.toString()));
            XmlElement oracleWhereIncludeElement = new XmlElement("include");
            oracleWhereIncludeElement.addAttribute(new Attribute("refid", "Example_Where_Clause"));
            selectCriteria.addElement(oracleWhereIncludeElement);
            XmlElement orderIncludeElement = new XmlElement("if");
            orderIncludeElement.addAttribute(new Attribute("test", "orderParam != null"));
            orderIncludeElement.addElement(new TextElement("order by ${orderParam}"));
            selectCriteria.addElement(orderIncludeElement);
            XmlElement oracleFootIncludeElement = new XmlElement("include");
            oracleFootIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Foot"));
            selectCriteria.addElement(oracleFootIncludeElement);
        } else
        if(databaseType.contains("mysql"))
        {
            selectCriteria.addElement(new TextElement("select"));
            XmlElement baseColumnIncludeElement = new XmlElement("include");
            baseColumnIncludeElement.addAttribute(new Attribute("refid", "Base_Column_List"));
            selectCriteria.addElement(baseColumnIncludeElement);
            sb.append("from ").append(tableName);
            selectCriteria.addElement(new TextElement(sb.toString()));
            XmlElement oracleWhereIncludeElement = new XmlElement("include");
            oracleWhereIncludeElement.addAttribute(new Attribute("refid", "Example_Where_Clause"));
            selectCriteria.addElement(oracleWhereIncludeElement);
            XmlElement orderIncludeElement = new XmlElement("if");
            orderIncludeElement.addAttribute(new Attribute("test", "orderParam != null"));
            orderIncludeElement.addElement(new TextElement("order by ${orderParam}"));
            selectCriteria.addElement(orderIncludeElement);
            XmlElement mysqlLimitIncludeElement = new XmlElement("include");
            mysqlLimitIncludeElement.addAttribute(new Attribute("refid", "common.Pagination_Limit"));
            selectCriteria.addElement(mysqlLimitIncludeElement);
        }
        return selectCriteria;
    }

    public List contextGenerateAdditionalXmlFiles(IntrospectedTable introspectedTable)
    {
        if(!commonFile)
        {
            Document document = new Document("-//mybatis.org//DTD Mapper 3.0//EN", "http://mybatis.org/dtd/mybatis-3-mapper.dtd");
            XmlElement answer = new XmlElement("mapper");
            document.setRootElement(answer);
            answer.addAttribute(new Attribute("namespace", "common"));
            if(databaseType.contains("oracle"))
            {
                answer.addElement(getOracleHead());
                answer.addElement(getOracleFoot());
            } else
            if(databaseType.contains("mysql"))
                answer.addElement(getMysqlLimit());
            GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties.getProperty("fileName", "CommonMapper.xml"), context.getSqlMapGeneratorConfiguration().getTargetPackage(), context.getSqlMapGeneratorConfiguration().getTargetProject(), false);
            List files = new ArrayList(1);
            files.add(gxf);
            commonFile = true;
            return files;
        } else
        {
            return null;
        }
    }

    private XmlElement getOracleHead()
    {
        XmlElement answer = new XmlElement("sql");
        answer.addAttribute(new Attribute("id", "Pagination_Head"));
        XmlElement outerisNotEmptyElement = new XmlElement("if");
        outerisNotEmptyElement.addAttribute(new Attribute("test", "startIndex != null and endIndex != null"));
        outerisNotEmptyElement.addElement(new TextElement("<![CDATA[ select * from ( select row_.*, rownum rn from ( ]]>"));
        answer.addElement(outerisNotEmptyElement);
        return answer;
    }

    private XmlElement getOracleFoot()
    {
        XmlElement answer = new XmlElement("sql");
        answer.addAttribute(new Attribute("id", "Pagination_Foot"));
        XmlElement outerisNotEmptyElement = new XmlElement("if");
        outerisNotEmptyElement.addAttribute(new Attribute("test", "startIndex != null and endIndex != null"));
        outerisNotEmptyElement.addElement(new TextElement("<![CDATA[ ) row_ where rownum <= #{endIndex} ) where rn > #{startIndex} ]]>"));
        answer.addElement(outerisNotEmptyElement);
        return answer;
    }

    private XmlElement getMysqlLimit()
    {
        XmlElement answer = new XmlElement("sql");
        answer.addAttribute(new Attribute("id", "Pagination_Limit"));
        XmlElement outerisNotEmptyElement = new XmlElement("if");
        outerisNotEmptyElement.addAttribute(new Attribute("test", "pageOffset != null and pageSize != null"));
        outerisNotEmptyElement.addElement(new TextElement("<![CDATA[ limit #{pageOffset} , #{pageSize} ]]>"));
        answer.addElement(outerisNotEmptyElement);
        return answer;
    }

    private String databaseType;
    private boolean commonFile;
}
